home *** CD-ROM | disk | FTP | other *** search
/ NetNews Offline 2 / NetNews Offline Volume 2.iso / news / comp / lang / c-part1 / 3322 < prev    next >
Encoding:
Internet Message Format  |  1996-08-05  |  1.7 KB

  1. Path: cymbal.aix.calpoly.edu!not-for-mail
  2. From: dstubbs@cymbal.aix.calpoly.edu (Dan Stubbs)
  3. Newsgroups: comp.lang.c
  4. Subject: Re: quick decision: is n a power of 2?
  5. Date: 27 Jan 1996 12:48:27 -0800
  6. Organization: California Polytechnic State University, San Luis Obispo
  7. Message-ID: <4ee32r$18e1@cymbal.aix.calpoly.edu>
  8. References: <Pine.OSF.3.91.960119114608.18779E-100000@io.UWinnipeg.ca> <4e1aeb$1gl8@cymbal.aix.calpoly.edu> <822516891snz@genesis.demon.co.uk> <4e8asi$ehg@ns.RezoNet.NET>
  9. NNTP-Posting-User: dstubbs@cymbal.aix.calpoly.edu
  10.  
  11. In article <4e8asi$ehg@ns.RezoNet.NET>, Ray Dunn <ray@ultimate-tech.com> wrote:
  12. >In referenced article, Lawrence Kirby says...
  13.  
  14. >>>int is_power_of_two(int k) {       /* 1.0  1.0  1.0  1.0 */
  15. >>>   if (k <= 0) return 0;
  16. >>>   return (!(k & (k-1)));
  17. >>>}
  18. >>>int is_power_of_two(int k) {       /*  1.1  1.0  1.1  1.0 */
  19. >>>   if (k <= 0) return 0;
  20. >>>   return ((k & (k-1))  == 0);
  21. >>>}
  22. >>I wouldn't be too impressed these days with a compiler that generates 
  23. >>less efficient code for ((k & (k-1) == 0) than (!(k & (k-1))
  24. >
  25. >Yes, that was my reaction too, but looking at it, the differences are 
  26. >in the noise level for this sort of timing which usually varies quite a 
  27. >bit - especially when using a test that only runs for 1 second.
  28.  
  29. When that data was posted it was pointed out that it was normalzed. For
  30. example, the times needed by four techniques (two of them shown above) to
  31. check the first 5,000,000 integers to see which of them is a power of 2
  32. was  185  1,333  167  341. The difference between 167 and 185 comes from
  33. the difference in how the compiler (set to its highest optimization level)
  34. treated the two expressions noted above. Unfortunately, I think there is
  35. a real difference. 
  36.  
  37.  
  38.  
  39.  
  40.  
  41.